<?php
include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
//Xoa het du lieu cac bang hien thi
$sql = "TRUNCATE TABLE liveboard";
mysql_query($sql);
$sql = "TRUNCATE TABLE currentmatch";
mysql_query($sql);
$sql = "TRUNCATE TABLE currentprice";
mysql_query($sql);

//Dien gia tri ban dau vao
$sql = "SELECT * FROM stock";
if(!($result = mysql_query($sql))) die(mysql_error());
while($row = mysql_fetch_array($result))
{
	$price = floatval($row['Price']);
	$pricemax = $price * 1.1;
	$pricemin = $price * 0.9;
	$sql = "INSERT INTO liveboard (Code, Price, PriceMax, PriceMin) VALUES ('".$row['Code']."','".$row['Price']."','$pricemax','$pricemin')";
	mysql_query($sql);
	$sql = "INSERT INTO currentmatch (StockID, PriceBegin) VALUES (".$row['StockID'].",'".$row['Price']."')";
	mysql_query($sql);
	$sql = "INSERT INTO currentprice (StockID, Average, High, Low) VALUES (".$row['StockID'].",'0','0','0')";
	mysql_query($sql);
}

include $_SERVER['DOCUMENT_ROOT'].'scue'.'/model/stock.php';
$code = "DPM";
$stock = new Stock();
$id = $stock->findIDStockByCode($code);
//Buy
$sql = "SELECT Price, SUM(Amount) FROM buyLO WHERE stockId = ".$id." GROUP BY Price ORDER BY Price DESC";
if(!($result = mysql_query($sql))) die(mysql_error());
for ($in = 1; $in <= 3; $in++)
{
	$price = "0";
	$amount = "0";
	if ($row = mysql_fetch_array($result))
	{
		$price = $row['Price'];
		$amount = $row['SUM(Amount)'];
	}
	$sql = "UPDATE liveboard SET BuyPrice".$in." = '".$price."', BuyAmount".$in." = '".$amount."' WHERE code = '".$code."'";
	mysql_query($sql);

}
//Sell
$sql = "SELECT Price, SUM(Amount) FROM sellLO WHERE stockId = ".$id." GROUP BY Price ORDER BY Price ASC";
if(!($result = mysql_query($sql))) die(mysql_error());
for ($in = 1; $in <= 3; $in++)
{
	$price = "0";
	$amount = "0";
	if ($row = mysql_fetch_array($result))
	{
		$price = $row['Price'];
		$amount = $row['SUM(Amount)'];
	}
	$sql = "UPDATE liveboard SET SellPrice".$in." = '".$price."', SellAmount".$in." = '".$amount."' WHERE code = '".$code."'";
	mysql_query($sql);

}
//Statistic currentmatch
$stock = new Stock();
$stockid = $stock->findIDStockByCode($code);
//Statistic currentmatch
$sql = "SELECT  Compare, Price, Amount FROM currentmatch WHERE StockID = ".$stockid;
if(!($result = mysql_query($sql))) die(mysql_error());
else
{
	$row = mysql_fetch_array($result);
	$sql = "UPDATE liveboard SET Diff= '".$row['Compare']."', DiffPrice= '".$row['Price']."', DiffAmount= '".$row['Amount']."' WHERE code = '".$code."'";
	mysql_query($sql);

}

//Statistic currentPrice
$sql = "SELECT  Average, High, Low FROM currentprice WHERE StockID = ".$stockid;
if(!($result = mysql_query($sql))) die(mysql_error());
else
{
	$row = mysql_fetch_array($result);
	$sql = "UPDATE liveboard SET Average= '".$row['Average']."', High= '".$row['High']."', Low= '".$row['Low']."' WHERE code = '".$code."'";
	mysql_query($sql);

}
?>